<?php

class Create extends Controller {

	function Create() {
		parent::Controller();
	}

	function _mkdate($d) {
		$parts = explode('/', $d);
		if(count($parts) != 3) {
			return '';
		}
		return $parts[2].'-'.$parts['1'].'-'.$parts[0];
	}

	function index() {
		if( !$this->Account->is_admin() && !$this->Account->is_manager() ) {
			redirect( '', 'location');
		}
		if( !isset($this->project_id) ) {
			$this->project_id = 0;
		}
		if( isset($_POST['submit']) ) {
			$start_date = $this->_mkdate($this->input->post('start_date'));
			$end_date = $this->_mkdate($this->input->post('end_date'));
			
			$title = trim($this->input->post('title'));
			$description = trim($this->input->post('description'));

			$project_id = $this->input->post('project_id');

			if(empty($title)) {
				$this->error = 'Please enter a title';

			} else if(empty($description)) {
				$this->error = 'Please enter a description';

			} else if(empty($start_date)) {
				$this->error = 'Select a start date';

			} else if(empty($end_date)) {
				$this->error = 'Select an end date';

			} else {
				$this->Tasks->create( $project_id, 
					$this->Account->account_id, 
					$title,
					$description, 
					$start_date, 
					$end_date, 
					$this->Tasks->NEW_TASK );
				redirect( 'project/view/id/'.$this->input->post('project_id'), 'location');
			}
		}
		$this->load->view('task/create');
	}

	function id($id) {
		assert(is_numeric($id));

		$this->project_id = $id;
		$this->index();
	}

}

?>
